<?php
namespace app\upapi\controller;

use think\Controller;
use app\common\util\WechatThirdUtil;
use app\common\model\WxUser as WxUserModel;
use app\common\model\AuthorWxUser as AuthorWxUserModel;

class Other extends Controller
{
    protected $author_wx_user_model;
    protected $config;
    protected $wx_user;

    public function _initialize(){
        parent::_initialize();
        $this->wx_user = new WxUserModel();
        $this->author_wx_user_model = new AuthorWxUserModel;
        $this->config = $this->wx_user->find();
    }

    /**
     * 公众号列表
     */
    public function getSubscriptionList(){
        $arr = $this->author_wx_user_model->field('id,wxname')->where('authorized',1)->select();
        if($arr){
            return json(['status' => 1 , 'msg' => 'ok' , 'subscription_list' => $arr]);
        }else{
            return json(['status' => -1 , 'msg' => 'ok' , 'subscription_list' => $arr]);
        }
    }

    /**
     * 公众号发消息
     */
    public function sendMsg(){
        if($this->request->isPost()){
            $data = $this->request->post();
            $wx_data = $this->author_wx_user_model->field('id,appid')->where('id',$data['id'])->find();
            $wechatObj = new WechatThirdUtil($this->config,$wx_data);
            $result = $wechatObj->sendMsgToOne($data['openid'], $data['content']);
            if($result) return json(['status' => 1]);
            return json(['status' => -1]);
        }
    }
}